package com.changxianggu.crawler.cloud.test;

import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.changxianggu.cloud.crawler.core.WebApplication;
import com.changxianggu.cloud.crawler.core.modules.storage.entity.CxgBook;
import com.changxianggu.cloud.crawler.core.modules.storage.service.ICxgBookService;
import com.changxianggu.cloud.crawler.core.utils.HttpUtil;
import com.changxianggu.cloud.crawler.core.utils.UserAgentUtil;
import com.google.gson.JsonObject;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {WebApplication.class})// 指定启动类
@Slf4j
public class Weixin {

    @Autowired
    ICxgBookService bookService;

    public static final String COOKIE = "wwrtx.xcx.sid=3ADEA483C22C425D4A4A82F58A526B0E6FB43579176CD7D16EF4D0F361692BBEA140BD128D884A7D68837776B8AC144B267C78434FAA2CDE1E0309A393E73BAD;wwrtx.xcx.vid=1688852913888042;wwrtx.xcx.token=CmoKBAgBEAASIDX9NJJ6PL8VbM6cAn6zavUCqTN7EcxQxfAIqCAgDQS3GkD%2BwliVrz7NsT4RG5nXZG3GouL%2FiOAikVV2hsxTMzTzZV5meWmOIt4slCxEiXt%2Ba42ChGnoCB6a3JDEZg2Qh%2F6AIggKBggAEAAYAA%3D%3D;wwrtx.xcx.type=228;wwrtx.xcx.buildid=0.0.1.1440";
    public static final String REFERER = "http://nwpup.iyuecloud.com/";
    public static final String URL_PRE = " http://press.cau.edu.cn";
    public static final String AUTH_SID = " 0ED807BFCEB4412CDA2BE8FED27F5052F5B5534117CACC0F8C4211B8E885D28053787EF394A6C4F4C3ED8D67217CEBCE075050E737423D4052AED581E32E8D7C";
    public static final String AUTH_VID = " 1688852913888042";
    public static final String AUTH_TOKEN = " CmoKBAgBEAASIBLSNBMhZVkyqRjQdeXDHYI5DQA2xt2v+Uk+KI/pGJoqGkAefPujJKPQDCEraeGPZKC0+RmEgLD/o9Bt8dcy/rgoKa2MUbTLkkREcMJVqckXdxoBesB1kZEtt4B8KVvJxKVTIggKBggAEAAYAA==";
    public static final Integer AUTH_TYPE = 228;
    public static final String AUTH_BUILDID = "0.0.1.1440";


    @Test
    public void initCrawler() throws Exception {
        String list1 = "F:\\企业战略.json";
//        String list2 = "F:\\2.txt";
        List<String> pathList = new ArrayList<>();
        pathList.add(list1);
//        pathList.add(list2);
        for (String list : pathList) {
            doCrawler(getCourseIdList(list));
        }
    }
    public JSONArray  getCourseIdList (String path) {
        FileReader file1Reader = new FileReader(path);
        String result = file1Reader.readString();
        JSONObject idsObj = JSONObject.parseObject(result);
        JSONArray list = JSON.parseArray(idsObj.getJSONObject("data").get("list").toString());
        return list;
    }
    public void doCrawler(JSONArray list) throws Exception {
        for (int i = 0; i < list.size(); i++) {
            JSONObject jsonTmp = list.getJSONObject(i);
            String url = "https://xcx.work.weixin.qq.com/course_hiweb/get_course_info?rand=616516483027";
            Map<String, Object> parameter = MapUtil.newHashMap(6);
            parameter.put("courseid",JSONObject.parseObject(jsonTmp.get("course").toString()).get("courseid"));
            parameter.put("auth_sid", AUTH_SID);
            parameter.put("auth_vid", AUTH_VID);
            parameter.put("auth_token", AUTH_TOKEN);
            parameter.put("auth_type", AUTH_TYPE);
            parameter.put("auth_buildid", AUTH_BUILDID);
            JSONObject jsonObj=new JSONObject(parameter);
            Map<String, Object> headers = MapUtil.newHashMap(5);
            headers.put("cookie", COOKIE);
            String result = HttpUtil.doPost(url, headers, jsonObj.toString());
            JSONObject jsonObject = JSONObject.parseObject(result);
            Integer errcode = jsonObject.getJSONObject("result").getInteger("errCode") ;
            if (errcode != null && errcode == 0) {
                // TODO 请求成功返回的json串存入文件
                // 课程基本信息
                JSONObject dataCourse = jsonObject.getJSONObject("data").getJSONObject("course");
                // 课程章节信息
                JSONObject dataCourseChapters = jsonObject.getJSONObject("data").getJSONObject("chapters");


                // TODO 转为实体  保存
                log.info("第" + i + "页数据保存成功");
                log.warn("--->" + i);
                }
            }
    }

//    public CxgBook getBookEntity() throws Exception {
//
//        return bookEntity;
//    }

    /**
     * 判断一个字符串是否都为数字
     *
     * @param strNum
     * @return
     */
    public boolean isDigit(String strNum) {
        Pattern pattern = Pattern.compile("[0-9]{1,}");
        Matcher matcher = pattern.matcher((CharSequence) strNum);
        return matcher.matches();
    }

    /**
     * 截取数字
     *
     * @param content
     * @return
     */
    public String getNumbers(String content) {
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            return matcher.group(0);
        }
        return "";
    }
}
